#include "log.h"
#include <iomanip>
#include <iostream>
#include <sstream>
#include <chrono>

std::string Log::ROOT = ".";

Log::Log() : std::ostream(this) 
{
	std::string timeStr = getTime();
 	 std::stringstream log_file_str;
    log_file_str << ROOT << "/"
        << timeStr
		<< ".txt";

	log_file_.open(log_file_str.str(), std::ios::out);
}

Log::~Log()
{
    log_file_.close();
}

std::string Log::getTime()
{
	std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
	std::time_t nowTime = std::chrono::system_clock::to_time_t(now);

	/*auto duration_in_ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
	auto duration_in_s = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch());
	int ms = duration_in_ms.count() - duration_in_s.count() * 1000;*/

	tm* t = localtime(&nowTime);
	int year = t->tm_year + 1900;
	int month = t->tm_mon + 1;
	int day = t->tm_mday;
	int hh = t->tm_hour;
	int mm = t->tm_min;
	int ss = t->tm_sec;

    std::stringstream time_str;
    time_str << year << "-"
        << month << "-"
        << day << " "
        << hh << ":"
        << mm << ":"
        << ss;
	return time_str.str();
}